/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.TypeCompte;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Cyn
 */
@Stateless
@LocalBean
public class GestionnaireTypeCompte {
    @PersistenceContext(unitName = "ARTP3-ejbPU")
    private EntityManager em;
    
    public void persist(Object object) {
        em.persist(object);
    }
    
    public void ajouter(TypeCompte t){
        em.persist(t);
    }
    
    public TypeCompte update(TypeCompte t){
        return em.merge(t);
    }
    
    public void delete(TypeCompte t) {
        System.err.println("delete nom :"+t.getLibelleType());
        TypeCompte us = getTypeCompteById(t.getId());
        em.remove(us);
    }
    
    public List<TypeCompte> getAllTypeCompte(){
        Query query = em.createNamedQuery("TypeCompte.findAll");
        return query.getResultList();
    }
    
    public TypeCompte getTypeCompteById(Long i){
        Query query = em.createNamedQuery("TypeCompte.findById");
        query.setParameter("typeid", i);        
        try{            
            System.out.println("==SessionBean==");
            return (TypeCompte) query.getSingleResult(); 
        }catch(Exception e){
            System.out.println("Type Compte inexistant");
            return null;
        }
    }
    
    public TypeCompte getTypeCompteByLibelle(Long i){
        Query query = em.createNamedQuery("TypeCompte.findByLibelle");
        query.setParameter("lib", i);        
        try{            
            System.out.println("==SessionBean==");
            return (TypeCompte) query.getSingleResult(); 
        }catch(Exception e){
            System.out.println("Type Compte inexistant");
            return null;
        }
    }
    
}
